package com.system.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.system.vo.SysMenuVO;

public class MenuAddChildrenUtil {
	public static  List<SysMenuVO> toTree(List<SysMenuVO> lst){
		if (lst == null || lst.size() == 0) {
			return null;
		}
		List<SysMenuVO> tree = new ArrayList<SysMenuVO>();
		for(SysMenuVO vo:lst){
			if (StringUtils.isEmpty(vo.getParMenuId())) {
				tree.add(vo);
			}
		}
		for(SysMenuVO node:tree){
			node.setChildrenList(findChildren(node, lst));
		}
		return tree;
	}
	
	public  static  List<SysMenuVO> findChildren(SysMenuVO parent, List<SysMenuVO> all){
		List<SysMenuVO> child = new ArrayList<SysMenuVO>();
		for(SysMenuVO item:all){
			if (parent.getMenuId()!=null&&parent.getMenuId().equals(item.getParMenuId())) {
				item.setChildrenList(findChildren(item, all));;
				child.add(item);
			}
		}
		return child;
	}
}
